/*************************************************************************
	> File Name: command.h
	> Author: ma6174
	> Mail: ma6174@163.com 
	> Created Time: 2021年12月08日 星期三 21时27分28秒
 ************************************************************************/

#include<iostream>
#include <string>
#include <list>
#include <typeinfo>
#include <chrono>
#include <time.h>
#include <algorithm>

struct Barbecuer
{
	void bakeMutton()
	{
		std::cout << "烤羊肉串!" << std::endl;
	}

	void bakeChickenWing()
	{
		std::cout << "烤鸡翅!" << std::endl;
	}
};

//命令类
class Command
{
public:
	Command() = default;
	Command(Barbecuer re) : receiver(re) {}

	virtual void executeCommand() = 0;

protected:
	Barbecuer receiver;
};

class BakeMuttonCommand : public Command
{
public:
	using Command::Command;

	virtual void executeCommand()
	{
		receiver.bakeMutton();
	}
};

class BakeChickenWingCommand : public Command
{
	using Command::Command;

	virtual void executeCommand()
	{
		receiver.bakeChickenWing();
	}
};

//服务员类
class Waiter
{
public:
	void setOrder(Command* cmd)
	{
		if (std::string(typeid(*cmd).name()) == "22BakeChickenWingCommand")
			std::cout << "今天没有烤鸡翅，点个其他的吧?" << std::endl;
		else
			this->orders.push_back(cmd);
	}

	void cancelOrder(Command* cmd)
	{
		auto tp = std::chrono::system_clock::now();
		std::time_t t = std::chrono::system_clock::to_time_t(tp);
		std::string ts = std::ctime(&t);
		if (std::find(orders.cbegin(), orders.cend(), cmd) != orders.cend())
		{
			this->orders.remove(cmd);
			std::cout << "取消订单　" << std::string(typeid(*cmd).name()).substr(2) << ts << std::endl;
		}
	}

	void notify()
	{
		for(auto elem : orders)
			elem -> executeCommand();
	}

private:
	std::list<Command*> orders;
};
